package team.star.werespos.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import team.star.werespos.model.vo.ResultVO;
import team.star.werespos.model.entity.FoodInformation;
import team.star.werespos.service.FoodInformationService;
import team.star.werespos.util.Result;

import java.util.List;

/**
 * FoodInformationController class<br/>
 *
 * @author CAI
 * @since 2019/03/25
 */
@Slf4j
@RestController
@RequestMapping("/food/info")
public class FoodInformationController {
    @Autowired
    private FoodInformationService foodInformationService;

    /**
     * json格式，要求传递 FoodInformation
     *
     * @param foodInformation
     * @return
     */
    @PostMapping("/new")
    public ResultVO<?> insertOne(@RequestBody FoodInformation foodInformation) {
        foodInformationService.insertOne(foodInformation);
        return Result.success();
    }


    /**
     * 获取商品详情
     *
     * @return
     */
    @GetMapping("/{foodId}/profile")
    public ResultVO<FoodInformation> getProfile(@PathVariable Integer foodId) {
        return Result.success(
                foodInformationService.getOne(foodId)
        );
    }

    /**
     * 获取商品类别列表
     *
     * @return
     */
    @GetMapping("/list")
    public ResultVO<List<FoodInformation>> listFood(Integer categoryId) {
        return Result.success(
                foodInformationService.listFood(categoryId)
        );
    }

    /**
     * 修改商品类别，只能改 categoryName 和 categoryType
     * {categoryId} 表示指定的类别的id，前端传递时实际形如：/123/update
     */
    @PostMapping("/{foodId}/update")
    public ResultVO<?> updateById(
            @PathVariable Integer foodId,
            @RequestBody FoodInformation foodInformation) {
        foodInformation.setFoodId(foodId);
        foodInformationService.updateById(foodInformation);
        return Result.success();
    }

    /**
     * 删除商品类别
     */
    @GetMapping("/{foodId}/delete")
    public ResultVO<?> deleteById(@PathVariable Integer foodId) {
        foodInformationService.deleteById(
                new FoodInformation().setFoodId(foodId)
        );
        return Result.success();
    }


}
